<!-- 下载页面 -->
{include 'admin/layout/header_list'}
<link rel="stylesheet" href="__ADMIN_STATIC__/css/pop_window.css?random={extendSetting('random')}">
<div class="downLoadList">
    <h4>下载列表</h4>
    <div class="downLoadListTable">
        <table>
            <col>
            <col width="120px">
            <col width="120px">
            <col width="120px">
            <tr class="downloadList-item">
                <td class="th"><span>文件名</span></td>
                <td class="th"><span>文件大小</span></td>
                <td class="th"><span>下载速度</span></td>
                <td class="th"><span>下载进度</span></td>
            </tr>
            {if isset($request['incUp']) && $request['incUp']=="0"}
                {foreach $listData $key $val}
                <tr class="downloadList-item">
                    <td>
                        <span>{basename($val['completePackageUrl'])}</span>
                        <input class="file" type="hidden" data-project="{$val['project']}" data-filesize="0" data-isover="0" data-exec="0" data-nums="0" data-stage="{$val['stage']}"  value="{urlencode($val['completePackageUrl'])}" />
                        <input class="file" type="hidden" data-project="{$val['project']}" data-filesize="0" data-isover="0" data-exec="0" data-nums="0" data-stage="{$val['stage']}" value="{urlencode($val['completePackageMd5Url'])}" />
                    </td>
                    <td>{php echo app('ltcmsFormat')->byte($val['completePackageSize']);}</td>
                    <td><span class="rate">---</span></td>
                    <td><span class="progress">0%</span></td>
                </tr>
                {/foreach}
            {else}
                {foreach $listData $key $val}
                <tr class="downloadList-item">
                    <td>
                        <span>{basename($val['packageUrl'])}</span>
                        <input class="file" type="hidden" data-project="{$val['project']}" data-filesize="0" data-isover="0" data-exec="0" data-nums="0" data-stage="{$val['stage']}"  value="{urlencode($val['packageUrl'])}" />
                        <input class="file" type="hidden" data-project="{$val['project']}" data-filesize="0" data-isover="0" data-exec="0" data-nums="0" data-stage="{$val['stage']}" value="{urlencode($val['packageMd5Url'])}" />
                    </td>
                    <td>{php echo app('ltcmsFormat')->byte($val['packageSize']);}</td>
                    <td><span class="rate">---</span></td>
                    <td><span class="progress">0%</span></td>
                </tr>
                {/foreach}
            {/if}
        </table>
    </div>
</div>
<input type="hidden" class="installId" value="{if isset($request['id'])}{$request['id']}{/if}" />
<input type="hidden" class="installIncUp" value="{if isset($request['incUp'])}{$request['incUp']}{/if}" />
<script>
    $(function (){
        download();
    });

    /**
     * 检测是否可进行安装2
     */
    function checkInstall(){
        //检测安装
        var install=1;
        $(".downLoadList").find(".file").each(function (index,ele){
            var isover=$(ele).data("isover");
            if(isover=="0"){
                install=0;
            }
        });
        if(install==1){
            //执行安装
            var installId=$(".installId").val();
            var installIncUp=$(".installIncUp").val();
            window.location.href="{LTCMS_ROUTE_PREFIX}/systemUpgrade/install?id="+installId+"&incUp="+installIncUp;
        }
    }

    function download (){
        //检测下载列表，没下载完的继续下载,同时下载几个
        var process_nums=2;
        var execNums=0;
        var url="{LTCMS_ROUTE_PREFIX}/systemUpgrade/downloadFile";
        $(".downLoadList").find(".file").each(function (index,ele){
            var isOver=$(ele).data("isover");
            var exec=$(ele).data("exec");
            var val=$(ele).val();
            if(execNums <= process_nums){
                if(isOver=="0" && exec=="0"){
                    //计数加1
                    execNums+=1;

                    //进行下载
                    var downUrl=$(ele).val();
                    var fileSize=$(ele).data("filesize");
                    var project=$(ele).data("project");
                    var stage=$(ele).data("stage");
                    var split_nums=$(ele).data("nums");
                    $(ele).data("exec","1");

                    $.ajax({
                        processData:false,
                        contentType :false,
                        type: "get",
                        url: url+"?downUrl="+downUrl+"&project="+project+"&stage="+stage+"&num="+split_nums+"&fileSize="+fileSize,
                        async: true,
                        dataType: "jsonp",
                        timeout: 0,
                        success:function(data)
                        {
                            var conError=1;
                            if(typeof data == "undefined")
                            {
                                conError=2;
                            }
                            else
                            {
                                if(!(typeof data == "object"))
                                {
                                    data=eval("("+data+")");
                                }
                            }
                            if(data.success){
                                var nums=$(ele).data("nums");
                                $(ele).data("nums",nums+1);
                                $(ele).data("filesize",data.data.fileSize);
                                $(ele).parents(".downloadList-item").find(".progress").html(data.data.progress);
                                $(ele).parents(".downloadList-item").find(".rate").html(data.data.rate);

                                if(data.data.isOver == "1"){
                                    $(ele).data("isover","1");
                                }
                            }else{
                                //下载失败
                                $(ele).parents(".downloadList-item").find(".progress").html("失败");
                                $(ele).data("isover","1");
                            }
                        },
                        complete:function(xhr)
                        {
                            $(ele).data("exec","0");
                            execNums-=1;
                            download();
                        }
                    });
                }
            }
        });
        checkInstall(); //检测是否安装
    }
    function sleep(time){
        var timeStamp=new Date().getTime();
        var endTime=timeStamp + time;
        while(true){
            var cutTimeStamp=new Date().getTime();
            if(cutTimeStamp > endTime){
                break;
            }
        }
    }
</script>
{include 'admin/layout/footer'}